package com.dxyxx.framework;

import java.util.HashMap;

import org.apache.http.Header;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.dxyxx.R;
import com.dxyxx.android.setting.UserPreferences;
import com.dxyxx.pojo.User;
import com.dxyxx.utils.BarcodeCreater;
import com.http.HttpUtil;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.ta.utdid2.android.utils.StringUtils;

public class ScanActivity extends Activity implements OnClickListener {
	private int width;
	private TextView tvv;
	private UserPreferences userPreferences;
	private User user;
	private ImageView refreshImage;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_scan);
		findViewById(R.id.backImageButton).setOnClickListener(this);

		refreshImage = (ImageView) findViewById(R.id.nextImageButton);
		refreshImage.setImageResource(R.drawable.refresh_normal);
		refreshImage.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {

				callNetwork();
			}
		});
		TextView titleView = (TextView) findViewById(R.id.title);
		titleView.setText("我的电子会员卡");
		titleView.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
		 
		tvv = (TextView) findViewById(R.id.networkWord);
		HashMap<String, String> params0 = new HashMap<String, String>();
		//		HttpTask task_word = new HttpTask(mListner, HttpRequestID.GET.WORD, params0);
		//		HttpExecutor.request(task_word);
		//		tvv.setText("");
		anim = AnimationUtils.loadAnimation(ScanActivity.this, R.anim.round_loading);
		// 得到屏幕宽度。
		WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);

		width = wm.getDefaultDisplay().getWidth();
		userPreferences = UserPreferences.getInstance(getApplicationContext());
		user = userPreferences.getCurrentUser();

		 
		 
		//		String smscode = edtVerify.getText().toString();
		HashMap<String, String> params = new HashMap<String, String>();

		if (StringUtils.isEmpty(user.getPhone())) {
			Toast.makeText(getApplicationContext(), "手机号或者验证码不能为空", 1).show();
			return;
		} // 得到手机号..生成验证码
		callNetwork();
	}

	 

	private void createImgage(String content) {
		// String content = "az1520196726723";
		Bitmap bitmap = null;
		ImageView iv = (ImageView) findViewById(R.id.scanResult);
		bitmap = BarcodeCreater.creatBarcode(getApplicationContext(), content, width - 40, (width - 40) / 2, false);
		// Drawable drawable = new BitmapDrawable(bitmap);
		iv.setImageBitmap(bitmap);
		// iv.setImageDrawable(drawable);;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.backImageButton:
			finish();
			break;
		 
		default:
			break;
		}
	}

	private Animation anim;

	private void callNetwork() {
		refreshImage.startAnimation(anim);
		refreshImage.setClickable(false);// 禁止重复发送。

		String url = "phones/SEND_PHONE_FOR_SCAN";
		HttpUtil.get(url, new AsyncHttpResponseHandler() {

			@Override
			public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
				createImgage(new String(arg2));
				refreshImage.setClickable(true);
				refreshImage.clearAnimation();
			}

			@Override
			public void onFailure(int arg0, Header[] arg1, byte[] arg2, Throwable arg3) {
				System.out.println("失败了" + new String(arg2));

				refreshImage.setClickable(true);
				refreshImage.clearAnimation();

			}
		});

	}

}
